/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.editor.model;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class CogroupOperator extends Operator {
    public void GeneratePigLatinStatement(final StringBuffer str) {
        Display display = Display.getCurrent();
        // final StringBuffer str = new StringBuffer();
        final Shell shell = new Shell(display);
        shell.setText("COGROUP");
        // createContents(shell);

        shell.setLayout(new GridLayout(11, false));

        // Create the entry field for the message

        final Text identifier = new Text(shell, SWT.NONE);
        new Label(shell, SWT.NONE).setText(" = COGROUP ");

        final Text cogroupSpec = new Text(shell, SWT.BORDER);
        Rectangle r = cogroupSpec.getBounds();
        cogroupSpec.setBounds(r.x, r.y, Math.max(r.width, 25), r.height);

        // message.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Create the label to show the return from the open call
        // new Label(shell, SWT.NONE).setText("Return:");
        /*
         * final Label returnVal = new Label(shell, SWT.NONE);
         * returnVal.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
         */

        // Create the button and event handler
        // to display the message box
        Button button = new Button(shell, SWT.PUSH);
        button.setText("Add Operator");

        button.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                // Clear any previously returned value
                // returnVal.setText("");

                str.append(identifier.getText());
                str.append(" = COGROUP ");
                str.append(cogroupSpec.getText());
                str.append(";");
                shell.dispose();
            }
        });

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}
